/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.feed.impl;

import java.util.List;


/**
 * Modifiable, not thread-safe implementation of a {@link ICollectionFeed}.
 * <p>
 * An instance must not be modified after it has been handled over to another
 * method.
 * </p>
 * <p>
 * Note: After construction, an instance is in an illegal state: 
 * {@link #getEntries()} throws an {@link IllegalStateException} and 
 * {@link #getFragmentsEntry()} / {@link #getSnapshotsEntry()} return <tt>null</tt>.
 * </p>
 * <p>
 * {@link #addEntry(ILinkAwareEntity)} throws a {@link UnsupportedOperationException}, 
 * use {@link #setFragmentsEntry(ILinkAwareEntity)} and 
 * {@link #setSnapshotsEntry(ILinkAwareEntity)}.
 * </p>
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
final class CollectionFeed extends AbstractFeed<ILinkAwareEntity> 
    implements ICollectionFeed {

    public CollectionFeed(String id, String title, long updated) {
        super(id, title, updated);
        _entries.add(null);
        _entries.add(null);
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.feed.ICollectionFeed#getFragmentsEntry()
     */
    @Override
    public ILinkAwareEntity getFragmentsEntry() {
        return _entries.get(0);
    }

    /**
     * Sets the fragments entry.
     *
     * @param fragmentsEntry The fragments entry.
     */
    public void setFragmentsEntry(ILinkAwareEntity fragmentsEntry) {
        _entries.set(0, fragmentsEntry);
    }

    /**
     * Sets the fragments entry.
     *
     * @param id The identifier, an IRI.
     * @param title The title.
     * @param updated The updated value.
     */
    public void setFragmentsEntry(final String id, final String title, final long updated) {
        setFragmentsEntry(new LinkAwareEntry(id, title, updated));
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.feed.ICollectionFeed#getSnapshotsEntry()
     */
    @Override
    public ILinkAwareEntity getSnapshotsEntry() {
        return _entries.get(1);
    }

    /**
     * Sets the snapshots entry.
     *
     * @param snapshotsEntry The snapshots entry.
     */
    public void setSnapshotsEntry(ILinkAwareEntity snapshotsEntry) {
        _entries.set(1, snapshotsEntry);
    }

    /**
     * Sets the snapthots entry.
     *
     * @param id The identifier, an IRI.
     * @param title The title.
     * @param updated The updated value.
     */
    public void setSnapshotsEntry(final String id, final String title, final long updated) {
        setSnapshotsEntry(new LinkAwareEntry(id, title, updated));
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.feed.impl.AbstractFeed#addEntry(java.lang.Object)
     */
    @Override
    public void addEntry(ILinkAwareEntity entry) {
        throw new UnsupportedOperationException();
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.feed.impl.AbstractFeed#getEntries()
     */
    @Override
    public List<ILinkAwareEntity> getEntries() {
        if (getFragmentsEntry() == null
                || getSnapshotsEntry() == null) {
            throw new IllegalStateException();
        }
        return super.getEntries();
    }


    private static final class LinkAwareEntry implements ILinkAwareEntity {

        private final String _id;
        private final String _title;
        private final long _updated;

        private LinkAwareEntry(final String id, final String title, final long updated) {
            if (id == null) {
                throw new IllegalArgumentException("The identifier must not be null");
            }
            if (title == null) {
                throw new IllegalArgumentException("The title must not be null");
            }
            _id = id;
            _title = title;
            _updated = updated;
        }

        @Override
        public String getId() {
            return _id;
        }

        @Override
        public String getTitle() {
            return _title;
        }

        @Override
        public long getUpdated() {
            return _updated;
        }

        @Override
        public String getLink() {
            return _id;
        }

    }

}
